import complex_query from '@feidao/web/atom/nodejs/db/complex-query';
import { spaceid, systemid } from './atom/config';

interface Message {
}

/**
 * @description 使用sql函数count
 * @param
 */

export default async function atom(msg: Message, action_id: string, session_id: string) {
	const cquery = complex_query(action_id, spaceid, systemid);

	const res = await cquery.add_field('msz_test3', 'room', 'room')
		.add_fun('msz_test3', 'num', 'num_count', 'count')
		.groupby('msz_test3', 'room')
		.orderby('num_count')
		.exec();


	// const res = await cquery.add_field('factory', 'postcode', 'postcode')
	// 	.add_fun('factory', 'postcode', 'count', 'count')
	// 	.groupby('factory', 'postcode')
	// 	.orderby('count')
	// 	.exec();
	/**
	 * 错误示例-1
	 * 
	 * 报错：`ERROR: ORDER BY "type_no" is ambiguous Position: 128`
	 * 
	 * .add_fun(tablename: string, fieldname: string, alias: string, funname: 'count' | 'max' | 'min' | 'avg' | 'sum')
	 * 
	 * const res = await cquery.add_field('factory', 'postcode', 'postcode')
	 .add_fun('factory', 'postcode', 'postcode', 'count')
	 .groupby('factory', 'postcode')
	 .orderby('postcode')
	 .exec();
	 */

	return res;
}
